﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Text.RegularExpressions;

namespace Medianamik.Core.Security
{
    public class PasswordValidator
    {
        private MembershipProvider _provider;
        public PasswordValidator(MembershipProvider pProvider)
        {
            _provider = pProvider;
        }

        public PasswordValidationResult ValidatePassword(string password)
        {
            if (password.Length < _provider.MinRequiredPasswordLength)
            {
                return PasswordValidationResult.TooShort;
            }
            int n = 0;
            for (int i = 0; i < password.Length; i++)
            {
                if (!char.IsLetterOrDigit(password, i))
                {
                    n++;
                }
            }
            if (n < _provider.MinRequiredNonAlphanumericCharacters)
            {
                return PasswordValidationResult.TooLittleNonAlphanumericCharacters;
            }
            if ((_provider.PasswordStrengthRegularExpression.Length > 0) && !Regex.IsMatch(password, _provider.PasswordStrengthRegularExpression))
            {
                return PasswordValidationResult.DoesNotMatchRegularExpression;
            }
            return PasswordValidationResult.Success;
        }
    }
}
